# conding   : utf-8
# @FileName : ppt_split.py
# @Author   : 刘汕汕
# @Time     : 2025/3/24 10:30
from pptx import Presentation
import os

# 读取原始PPT文件
input_pptx = "credit_test.pptx"
prs = Presentation(input_pptx)

# 创建存储拆分PPT的目录
output_dir = "ppt_split_data"
os.makedirs(output_dir, exist_ok=True)

# 遍历每一页，创建新的PPT
for i, slide in enumerate(prs.slides):
    new_prs = Presentation()  # 创建一个新的PPT
    new_slide_layout = new_prs.slide_layouts[5]  # 选择一个空白布局
    new_slide = new_prs.slides.add_slide(new_slide_layout)

    # 复制幻灯片内容
    for shape in slide.shapes:
        if shape.has_text_frame:  # 复制文本
            text_frame = new_slide.shapes.add_textbox(
                shape.left, shape.top, shape.width, shape.height
            ).text_frame
            text_frame.text = shape.text_frame.text

    # 保存单独的PPT文件
    output_pptx = os.path.join(output_dir, f"slide_{i+1}.pptx")
    new_prs.save(output_pptx)

print(f"拆分完成，每页已保存到 {output_dir} 目录")
